package co.yixiang.modules.supplier.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import co.yixiang.modules.supplier.domain.SupplierGoodsSku;
import co.yixiang.modules.supplier.domain.SupplierGoodsSkuLog;
import co.yixiang.modules.supplier.service.SupplierGoodsSkuService;
import co.yixiang.modules.supplier.service.dto.SupplierGoodsSkuDto;
import co.yixiang.utils.*;
import co.yixiang.modules.supplier.repository.SupplierGoodsSkuLogRepository;
import co.yixiang.modules.supplier.service.SupplierGoodsSkuLogService;
import co.yixiang.modules.supplier.service.dto.SupplierGoodsSkuLogDto;
import co.yixiang.modules.supplier.service.dto.SupplierGoodsSkuLogQueryCriteria;
import co.yixiang.modules.supplier.service.mapper.SupplierGoodsSkuLogMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.annotation.Resource;


/**
* @author Walert
* @date 2021-03-27
*/
@Service
//@CacheConfig(cacheNames = "supplierGoodsSkuLog")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class SupplierGoodsSkuLogServiceImpl implements SupplierGoodsSkuLogService {

    @Resource
    private SupplierGoodsSkuLogRepository supplierGoodsSkuLogRepository;
    @Resource
    private SupplierGoodsSkuLogMapper supplierGoodsSkuLogMapper;
    @Resource
    private SupplierGoodsSkuService goodsSkuService;

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(SupplierGoodsSkuLogQueryCriteria criteria, Pageable pageable){
        Page<SupplierGoodsSkuLog> page = supplierGoodsSkuLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(supplierGoodsSkuLogMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<SupplierGoodsSkuLogDto> queryAll(SupplierGoodsSkuLogQueryCriteria criteria){
        return supplierGoodsSkuLogMapper.toDto(supplierGoodsSkuLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public SupplierGoodsSkuLogDto findById(Long id) {
        SupplierGoodsSkuLog supplierGoodsSkuLog = supplierGoodsSkuLogRepository.findById(id).orElseGet(SupplierGoodsSkuLog::new);
        ValidationUtil.isNull(supplierGoodsSkuLog.getId(),"SupplierGoodsSkuLog","id",id);
        return supplierGoodsSkuLogMapper.toDto(supplierGoodsSkuLog);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public SupplierGoodsSkuLogDto create(SupplierGoodsSkuLog resources) {
        return supplierGoodsSkuLogMapper.toDto(supplierGoodsSkuLogRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(SupplierGoodsSkuLog resources) {
        SupplierGoodsSkuLog supplierGoodsSkuLog = supplierGoodsSkuLogRepository.findById(resources.getId()).orElseGet(SupplierGoodsSkuLog::new);
        ValidationUtil.isNull( supplierGoodsSkuLog.getId(),"SupplierGoodsSkuLog","id",resources.getId());
        supplierGoodsSkuLog.copy(resources);
        supplierGoodsSkuLogRepository.save(supplierGoodsSkuLog);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            supplierGoodsSkuLogRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<SupplierGoodsSkuLogDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SupplierGoodsSkuLogDto supplierGoodsSkuLog : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("商品SKU_ID", supplierGoodsSkuLog.getSkuId());
            map.put("变更时间", supplierGoodsSkuLog.getChangeTime());
            map.put("上市时间", supplierGoodsSkuLog.getAddTime());
            map.put("退市时间", supplierGoodsSkuLog.getOutTime());
            map.put("价格有效时间", supplierGoodsSkuLog.getEffeTime());
            map.put("一级供货价(元)", supplierGoodsSkuLog.getFirstSourcePrice());
            map.put("一级售价(元)", supplierGoodsSkuLog.getFirstSalePrice());
            map.put("二级供货价(元)", supplierGoodsSkuLog.getSecondSourcePrice());
            map.put("二级售价(元)", supplierGoodsSkuLog.getSecondSalePrice());
            map.put("三级供货价(元)", supplierGoodsSkuLog.getThridSourcePrice());
            map.put("三级售价(元)", supplierGoodsSkuLog.getThridSalePrice());
            map.put("零售供货价(元)", supplierGoodsSkuLog.getSourcePrice());
            map.put("零售售价(元)", supplierGoodsSkuLog.getSalePrice());
            map.put("是否删除 0：否 1：是", supplierGoodsSkuLog.getIsDel());
            map.put("创建时间", supplierGoodsSkuLog.getCreateTime());
            map.put("创建人", supplierGoodsSkuLog.getCreateUser());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    public void make30dayPrice(Long skuId, Integer capacity){
        SupplierGoodsSkuDto goodsSku = goodsSkuService.findById(skuId);
        capacity = capacity * 100;
        Date now = DateUtil.parse(DateUtil.today());
        for (int i = 1;i <= 30; i++){
            DateTime dateTime = DateUtil.offsetDay(now, -i);
            long changeTime = dateTime.getTime();
            saveSkuLog(goodsSku,changeTime,capacity);
        }
    }

    private void saveSkuLog(SupplierGoodsSkuDto goodsSku,long changeTime, Integer capacity){
        Random random = new Random();
        SupplierGoodsSkuLog goodsSkuLog = new SupplierGoodsSkuLog();
        goodsSkuLog.setSkuId(goodsSku.getId());
        goodsSkuLog.setChangeTime(changeTime);
        goodsSkuLog.setAddTime(goodsSku.getAddTime());
        goodsSkuLog.setEffeTime(goodsSku.getEffeTime());
        goodsSkuLog.setOutTime(goodsSku.getOutTime());

        int num = random.nextInt(capacity);
        goodsSkuLog.setFirstSourcePrice(goodsSku.getFirstSourcePrice() + num);
        goodsSkuLog.setFirstSalePrice(goodsSku.getFirstSalePrice() + num);

        goodsSkuLog.setSecondSourcePrice(goodsSku.getSecondSourcePrice()+ num);
        goodsSkuLog.setSecondSalePrice(goodsSku.getSecondSalePrice()+ num);

        goodsSkuLog.setThridSourcePrice(goodsSku.getThridSourcePrice() + num);
        goodsSkuLog.setThridSalePrice(goodsSku.getThridSalePrice() + num);

        goodsSkuLog.setSourcePrice(goodsSku.getSourcePrice() + num);
        goodsSkuLog.setSalePrice(goodsSku.getSalePrice() + num);

        goodsSkuLog.setCreateTime(changeTime);
        goodsSkuLog.setCreateUser(SecurityUtils.getUsername());
        goodsSkuLog.setIsDel(0);
        supplierGoodsSkuLogRepository.save(goodsSkuLog);
    }


    public void saveSkuLog(SupplierGoodsSkuDto goodsSku,long changeTime){
        List<SupplierGoodsSkuLog> list = supplierGoodsSkuLogRepository.findByChangeTimeAndSkuId(changeTime,goodsSku.getId());
        SupplierGoodsSkuLog goodsSkuLog = new SupplierGoodsSkuLog();
        if(list != null && list.size() > 0){
            goodsSkuLog.copy(list.get(0));
        }else{
            goodsSkuLog.setCreateTime(changeTime);
            goodsSkuLog.setCreateUser("system");
            goodsSkuLog.setIsDel(0);
        }
        goodsSkuLog.setSkuId(goodsSku.getId());
        goodsSkuLog.setChangeTime(changeTime);
        goodsSkuLog.setAddTime(goodsSku.getAddTime());
        goodsSkuLog.setEffeTime(goodsSku.getEffeTime());
        goodsSkuLog.setOutTime(goodsSku.getOutTime());

        goodsSkuLog.setFirstSourcePrice(goodsSku.getFirstSourcePrice());
        goodsSkuLog.setFirstSalePrice(goodsSku.getFirstSalePrice());

        goodsSkuLog.setSecondSourcePrice(goodsSku.getSecondSourcePrice());
        goodsSkuLog.setSecondSalePrice(goodsSku.getSecondSalePrice());

        goodsSkuLog.setThridSourcePrice(goodsSku.getThridSourcePrice());
        goodsSkuLog.setThridSalePrice(goodsSku.getThridSalePrice());

        goodsSkuLog.setSourcePrice(goodsSku.getSourcePrice());
        goodsSkuLog.setSalePrice(goodsSku.getSalePrice());

        supplierGoodsSkuLogRepository.saveAndFlush(goodsSkuLog);
    }
}
